package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"k8s-platform/service"
	"log"
	"net/http"
)

type servicev1 struct{}

var Service servicev1

/*
{
	"name": "k8sservicetest",
	"namespace": "default",
	"type": "NodePort",
	"container_port": 80,
	"port": 8080,
	"node_port": 18080,
	"label": {
		"aaa":"bbbb"
	}
}
*/
//创建service
func (s *servicev1) CreateService(c *gin.Context) {
	//处理入参 匿名结构体 用于定义入参 get请求为form格式 其他请求为json
	params := &service.ServiceCreate{}

	//form使用Bind方法  json使用ShouldBindJSON
	if err := c.ShouldBindJSON(params); err != nil {
		log.Printf("Bind绑定参数失败：%s", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  fmt.Sprintf("Bind绑定参数失败：%s", err),
			"data": nil,
		})
		return
	}

	err := service.Service.CreateService(params)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"msg":  "create service success",
		"data": nil,
	})
}


//删除service
func (s *servicev1)DeleteService(c *gin.Context)  {
	params := new(struct {
		ServiceName string `json:"service_name" binding:"required"`
		Namespace  string `json:"namespace" binding:"required"`
	})

	//form使用Bind方法  json使用ShouldBindJSON
	if err := c.ShouldBindJSON(params); err != nil {
		log.Printf("Bind绑定参数失败：%s", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  fmt.Sprintf("Bind绑定参数失败：%s", err),
			"data": nil,
		})
		return
	}

	err := service.Service.DeleteService(params.ServiceName, params.Namespace)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"msg":  "delete service success",
		"data": nil,
	})
}

//获取 service 列表  支持过滤  分页  排序
func (s *servicev1) GetServices(c *gin.Context) {
	//处理入参 匿名结构体 用于定义入参 get请求为form格式 其他请求为json
	params := new(struct {
		FilterName string `form:"filter_name"`
		Namespace  string `form:"namespace"`
		Limit      int    `form:"limit"`
		Page       int    `form:"page"`
	})
	//form使用Bind方法  json使用ShouldBindJSON
	if err := c.Bind(params); err != nil {
		log.Printf("Bind绑定参数失败：%s", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  fmt.Sprintf("Bind绑定参数失败：%s", err),
			"data": nil,
		})
		return
	}
	data, err := service.Service.GetServices(params.FilterName, params.Namespace, params.Limit, params.Page)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"msg":  "get service list success",
		"data": data,
	})
}

//获取 daemonset 详情
func (s *servicev1) GetServiceDetail(c *gin.Context) {
	//处理入参 匿名结构体 用于定义入参 get请求为form格式 其他请求为json
	params := new(struct {
		ServiceName string `form:"service_name" binding:"required"`
		Namespace  string `form:"namespace" binding:"required"`
	})
	//form使用Bind方法  json使用ShouldBindJSON
	if err := c.Bind(params); err != nil {
		log.Printf("Bind绑定参数失败：%s", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  fmt.Sprintf("Bind绑定参数失败：%s", err),
			"data": nil,
		})
		return
	}

	data, err := service.Service.GetServiceDetail(params.ServiceName, params.Namespace)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"msg":  "get service detail success",
		"data": data,
	})
}
